package com.example.administrator.im.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.administrator.im.MyApplication;
import com.example.administrator.im.R;
import com.example.administrator.im.ui.activity.SettingActivity;

import java.util.List;

/**
 * create:2018/11/2 14:36
 * author: 姚鸿杰
 **/
public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.Holder> {

    private List<SettingActivity.RecyclerData> mList;
    private Context mContext;

    public SettingAdapter(List<SettingActivity.RecyclerData> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.recycler_setting, viewGroup, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int i) {

        final SettingActivity.RecyclerData data = mList.get(i);

        if (TextUtils.isEmpty(data.getTitle())) {
            holder.title.setVisibility(View.GONE);
        } else {
            holder.title.setText(data.getTitle());
        }
        holder.content.setText(data.getContent());

        SharedPreferences spf = MyApplication.getmContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        boolean show_content = spf.getBoolean("show_content", true);    //是否有打开来消息通知了显示
        boolean led = show_content && spf.getBoolean("led", true);  //led灯指示
        boolean shock = spf.getBoolean("shock", true);  //震动是否打开

        if (TextUtils.equals(data.getContent(), SettingActivity.SHOCK)) {
            holder.aSwitch.setChecked(shock);
        } else if (TextUtils.equals(data.getContent(), SettingActivity.SHOW_CONTENT)) {
            holder.aSwitch.setChecked(show_content);
        } else {
            holder.aSwitch.setChecked(led);
        }

        holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setting(holder.aSwitch, data.getContent(), isChecked);
            }
        });
    }

    private void setting(Switch aSwitch, String content, boolean isOpen) {
        if (TextUtils.equals(content, SettingActivity.SHOCK)) {
            updateSetting("shock", isOpen);
        } else if (TextUtils.equals(content, SettingActivity.SHOW_CONTENT)) {
            if (!isOpen) {
                updateSetting("led", isOpen);
                aSwitch.setChecked(isOpen);
            }
            updateSetting("show_content", isOpen);
        } else {
            if (isOpen) {
                updateSetting("show_content", isOpen);
                aSwitch.setChecked(isOpen);
            }
            updateSetting("led", isOpen);
        }
    }

    //更新设置
    private void updateSetting(String key, boolean isOpen) {
        //该功能暂时无法保存到数据库
        SharedPreferences.Editor editor = mContext.getSharedPreferences("user", Context.MODE_PRIVATE).edit();
        editor.putBoolean(key, isOpen);
        editor.apply();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        private TextView title, content;
        private Switch aSwitch;

        public Holder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title_setting);
            content = itemView.findViewById(R.id.content_setting);
            aSwitch = itemView.findViewById(R.id.switch_setting);
        }
    }
}
